//  ========== 
//  = 宾和   用户注册业务JS = 
//  ==========
var recommandPerson; //推荐码
var token;
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.loadData();
};
Main.prototype = {
	init: function() {
		recommandPerson = Util.requestURL('recommandPerson');

		function guid() {
			function s4() {
				return Math.floor((1 + Math.random()) * 0x10000)
					.toString(16)
					.substring(1);
			}
			return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
				s4() + '-' + s4() + s4() + s4();
		}
		token = guid();
	},
	even: function() {
		/**跳转登录页面*/
		$('#btn-toLogin').on('tap', function() {
			window.location.href = 'login.html?telphone=' + $("#telphone").val();
		});

		function refreshimg() {
			$("#imgCode").val("");
			var url = server_url + "/getCode.do";
			$('#btn-sendImg').attr("src", url + "?token=" + token + "&d=" + Math.random());
		}
		/**刷新图片验证码*/
		$('#btn-sendImg').on('tap', refreshimg);

		function checkPhone(phone) {
			if(!phone) {
				mui.toast("请输入手机号");
				return false;
			}
			if(!Verify.phone(phone)) {
				mui.toast(Verify.phoneMsg);
				return false;
			}
			return true;
		}

		function checkPassword(password) {
			if(!password) {
				mui.toast("请输入密码");
				return false;
			}
			if(!Verify.loginPassword(password)) {
				mui.toast(Verify.loginPasswordMsg);
				return false;
			}
			return true;
		}

		function sendMessageCode(telphone, imgCode) {
			//发送注册短信验证码
			var sendSmsUrl = server_url + "/codecreate.do";
			var password = $("#password").val();
			var param = {
				isMobile: isMobile,
				sms_code_type: 'bind_telphone',
				telphone: telphone,
				randomCode: Math.random(),
				memID: Util.memID(),
				token: token,
				imgCode: imgCode,
				graphicCode: imgCode
			}
			$.getJSON(sendSmsUrl, param, function(retData) {
				if(retData.status == 200) {
					mui.openWindow("registerSubmit.html?telphone=" + telphone + "&password=" + password + "&recommandPerson=" + recommandPerson);
				} else {
					mui.toast('验证码错误');
					refreshimg();
				}
			});
		}
		/** 提交注册*/
		$("#btn-checkPhone").on('tap', function() {
			var telphone = $("#telphone").val();
			var password = $("#password").val();
			if(!checkPhone(telphone) || !checkPassword(password)) {
				return false;
			}
			var imgCode = document.getElementById("imgCode").value;
			if(!imgCode || imgCode.length != 4) {
				Util.toast("请输入验证码");
				return false;
			}
			var checkPhoneUrl = server_url + "/checkPhonecreate.do";
			var param = {
				isMobile: isMobile,
				telphone: telphone
			}
			Util.loading();
			$.getJSON(checkPhoneUrl, param, function(retData) {
				if(!retData.success) {
					Util.closeLoading();
					/*if('手机号码已注册,请直接登陆' == retData.errMsg) {
						//询问框
						layer.open({
							content: '已有帐号,登录',
							btn: ['去登录', '取消'],
							yes: function(index) {
								window.location.href = 'login.html?telphone=' + telphone;
								layer.close(index);
							}
						});
					}else{
					}*/
					mui.toast(retData.errMsg);
				} else {
					Util.closeLoading();
					sendMessageCode(telphone, imgCode);
				}
			});
		});
	},
	loadData: function() {
		var url = server_url + "/getCode.do";
		$('#btn-sendImg').attr("src", url + "?token=" + token);
	}
};